package com.zhongkai.service.impl;

import com.mysql.jdbc.StringUtils;
import com.zhongkai.Bean.PageBean;
import com.zhongkai.app.service.IBaseDao;
import com.zhongkai.app.service.PageQuerDao;
import com.zhongkai.app.service.SupplierProductService;
import com.zhongkai.model.utils.TTHelp;
import com.zhongkai.model.utils.ZhongkaiResult;
import com.zhongkai.model3.Supplier;
import com.zhongkai.model3.SupplierProduct;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class SupplierProductServiceImpl implements SupplierProductService {

    @Autowired
    private IBaseDao iBaseDao;


    @Autowired
    private PageQuerDao pageQuerDao;


    /**
     * 添加产品
     *
     * @param supUuid  厂家uuid
     * @param supName  厂家名称
     * @param proName  产品名称
     * @param proPrice 产品单价
     * @param proUnit  产品单位
     */
    @Override
    public ZhongkaiResult addProduct(String supUuid, String supName, String proName, String proPrice, String proUnit) {
        if (StringUtils.isNullOrEmpty(supUuid) || StringUtils.isNullOrEmpty(supName) || StringUtils.isNullOrEmpty(proName)
                || StringUtils.isNullOrEmpty(proPrice) || StringUtils.isNullOrEmpty(proUnit)
                ) {
            return ZhongkaiResult.build(400, "数据不完整");
        }


        Supplier supplier = (Supplier) iBaseDao.get("from Supplier sp where sp.supUuid='" + supUuid + "'");

        if (supplier == null) {
            return ZhongkaiResult.build(400, "厂家不存在");
        }

        List<SupplierProduct> list = iBaseDao.find("from SupplierProduct sp where sp.supUuid='" + supUuid + "'");
        if (list != null) {
            for (SupplierProduct supplierProduct : list) {
                if (proName.equals(supplierProduct.getProName())) {
                    return ZhongkaiResult.build(400, "您已经在此厂家名下录入过此商品,如果有变动请您修改");
                }
            }
        }


        long millis = new DateTime().getMillis(); //获取当前系统时间戳

        String uuid = TTHelp.generate();

        System.out.println(uuid+"ConstraintViolationException");
        SupplierProduct supplierProduct = new SupplierProduct();

        supplierProduct.setProUuid(uuid);
        supplierProduct.setProName(proName);
        supplierProduct.setSupName(supName);
        supplierProduct.setSupUuid(supUuid);
        supplierProduct.setProPrice(proPrice);
        supplierProduct.setProUnit(proUnit);
        supplierProduct.setProTime(millis);

        iBaseDao.save(supplierProduct);

        return ZhongkaiResult.ok();
    }


    /**
     * 产品修改
     *
     * @param proUuid  产品uuid
     * @param supUuid  厂家uuid
     * @param supName  厂家名称
     * @param proName  产品名称
     * @param proPrice 产品单价
     * @param proUnit  产品单位
     */
    @Override
    public ZhongkaiResult updateProduct(String proUuid, String supUuid, String supName, String proName, String proPrice, String proUnit) {
        if (StringUtils.isNullOrEmpty(supUuid) || StringUtils.isNullOrEmpty(supName) || StringUtils.isNullOrEmpty(proName)
                || StringUtils.isNullOrEmpty(proPrice) || StringUtils.isNullOrEmpty(proUnit)
                ) {
            return ZhongkaiResult.build(400, "数据不完整");
        }


        SupplierProduct product = (SupplierProduct) iBaseDao.get("from SupplierProduct sp where sp.proUuid='" + proUuid + "'");

        if (product == null) {
            return ZhongkaiResult.build(400, "此产品不存在");
        }

        Supplier supplier = (Supplier) iBaseDao.get("from Supplier sp where sp.supUuid='" + supUuid + "'");

        if (supplier == null) {
            return ZhongkaiResult.build(400, "厂家不存在");
        }

        List<SupplierProduct> list = iBaseDao.find("from SupplierProduct sp where sp.supUuid='" + supUuid + "'");
        if (list != null) {
            for (SupplierProduct supplierProduct : list) {
                if (proName.equals(supplierProduct.getProName())) {

                    if (!proName.equals(product.getProName())) { //要修改的名称与之前一致不需要拦截 否则 需要拦截
                        return ZhongkaiResult.build(400, "您已经在此厂家名下录入过此商品,如果有变动请您修改");
                    }

                }
            }
        }

        product.setProName(proName);
        product.setSupName(supName);
        product.setSupUuid(supUuid);
        product.setProPrice(proPrice);
        product.setProUnit(proUnit);
        iBaseDao.update(product);

        return ZhongkaiResult.ok();
    }


    /**
     * 删除
     *
     * @param proUuid 产品uuid
     */
    @Override
    public ZhongkaiResult removeProduct(String proUuid) {


        SupplierProduct product = (SupplierProduct) iBaseDao.get("from SupplierProduct sp where sp.proUuid='" + proUuid + "'");

        if (product == null) {
            return ZhongkaiResult.build(400, "此产品不存在");
        }

        iBaseDao.delete(product);

        return ZhongkaiResult.ok();
    }


    /**
     * 产品列表
     *
     * @param page 页码
     * @param rows 数据条数
     * @param sup_uuid 供应商UUID
     */
    @Override
    public ZhongkaiResult listProduct(Integer page, Integer rows,String sup_uuid) {
        if (page == 0 || rows == 0 || StringUtils.isNullOrEmpty(sup_uuid)) {
            return ZhongkaiResult.build(400, "数据不完整");
        }

        PageBean pageBean = new PageBean();
        pageBean.setCurrentPage(page);
        pageBean.setPageSize(rows);
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(SupplierProduct.class);
        detachedCriteria.add(Restrictions.eq("supUuid", sup_uuid)); //增加条件
        detachedCriteria.addOrder(Order.desc("proTime")); //根据传入的字段进行降序排序

        pageBean.setDetachedCriteria(detachedCriteria);
        pageQuerDao.pageQuery(pageBean);
        return ZhongkaiResult.ok(pageBean);

    }
}
